//
//  AppDelegate.swift
//  SmartPenSwift
//
//  Created by tql on 2021/2/3.
//

import UIKit
import SmartPenSDK

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
//        [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
//        [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
//        [[UIBarButtonItem appearance] setTintColor:[UIColor blackColor]];
        
        NSLog("%@", BLELog.sdkVersion())
        UINavigationBar.appearance().backgroundColor = .white
        UINavigationBar.appearance().barTintColor = .white;
        UIBarButtonItem.appearance().tintColor = .black
        
        
//
//        var Soulut : Solution = Solution()
//        var Strings: String = "abbaca"
//    let result =     Soulut.removeDuplicates(Strings)
//
//        debugPrint("result=== \(result)")
//
//
//
//        let s : String = "(1+(4+5+2)-3)+(6+8)"
//
//        let res = Soulut.calculate3(s)
//        debugPrint("resresresaaaaa \(res)")
//        let ss : String = " 3+5 / 2"
//
//        let res2 = Soulut.calculate2(ss)
//        debugPrint("resresres222 \(res2)")
        
        return true
    }

    // MARK: UISceneSession Lifecycle
 
    
    
    


}


class Solution {
    func removeDuplicates(_ S: String) -> String {

        var s : [Character] = []
        for ch : Character in S {
            if ch == s.last {
                s.removeLast()
            }else{
                s.append(ch)
            }
        }
        
        debugPrint(s)
        return String(s)
//        return ""// Array.joined(s)
        
    }
    
    func calculate2(_ s : String) -> Int{
        let trimS : String = s.replacingOccurrences(of: " ", with: "")
        debugPrint("strings = \(trimS)")
        var stack : [Int] = []
        var num : Int = 0
        var preSign : Character = Character("+")
        let chars : [Character] = Array.init(trimS)
        let zero : Character = Character("0")

        for i : Int in 0..<chars.count {
            if chars[i].isNumber {
                num = num * 10 + Int( String(chars[i]).unicodeScalars.first!.value -   String(zero).unicodeScalars.first!.value)
            }
            if chars[i].isNumber == false || i == chars.count - 1 {
                if preSign == Character("+") {
                    stack.append(num)
                }else if preSign == Character("-"){
                    stack.append(-num)
                }else if preSign == Character("*"){
                    let lastNum : Int = stack.last!
                    stack.removeLast()
                    
//                    let last : Int = stack.dropLast(1).first as! Int
                    stack.append(lastNum * num)
                }else if preSign == Character("/"){
//                    let last : Int = stack.dropLast(1).first as! Int
                    let lastNum : Int = stack.last!
                    stack.removeLast()

                    stack.append(lastNum / num)
                }
                preSign = chars[i]
                num = 0
            }
            
        }
        var res : Int = 0
        while stack.count != 0 {
            let lastNum : Int = stack.last!
            stack.removeLast()
            
            res += lastNum
        }
        
        return res
        
        
        return 0
    }
    func calculate3(_ s: String) -> Int {
    
        var  res : Int = 0 , num : Int = 0, sign : Int = 1
        let trimS : String = s.replacingOccurrences(of: " ", with: "")
        var stack : [Int] = []
        let chars : [Character] = Array.init(trimS)
        let zero : Character = Character("0")

        for c : Character in trimS {
            debugPrint("c----\(c)")
            if c.isNumber == true {
                num = 10 * num +  Int( String(c).unicodeScalars.first!.value -   String(zero).unicodeScalars.first!.value)
            }else if c == Character("+") || c == Character("-") {
                res += sign * num
                num = 0
                sign = (c == Character("+")) ? 1 : -1
            }else if c == Character("(") {
                stack.append(res)
                stack.append(sign)
                res = 0
                sign = 1
            }else if c == Character(")"){
                res += sign * num
                num = 0
                let last : Int = stack.last!
                stack.removeLast()
                res *= last
                
                let last2 : Int = stack.last!
                stack.removeLast()
                res += last2
                debugPrint("res === \(res)")
            }
        }
        
        res += sign * num
        
        
        return res
    }
    
    func calculate(_ s: String) -> Int {
        var trimS : String = s.replacingOccurrences(of: " ", with: "")
        debugPrint("strings = \(trimS)")
        var stack : [Int] = []
        var sign : Int = 1
        var res : Int = 0
        var length : Int = trimS.count
        let zero : Character = Character("0")
        for var  i : Int in 0..<length {
            let ch : Character = Array(trimS)[i]
            if ch.isNumber {
                
                var cur : Int = Int( String(ch).unicodeScalars.first!.value -   String(zero).unicodeScalars.first!.value)
                
                while i + 1 < length  {
                    let nextch : Character = Array(trimS)[i+1]
                    if nextch.isNumber {
                        let nextCur : Int = Int( String(nextch).unicodeScalars.first!.value -   String(zero).unicodeScalars.first!.value)

                        cur = cur * 10 + nextCur
                        i = i + 1
                    }else{
//                        i = i + 1
                        break
                    }
                }
                res = res + sign * cur
            }else if ch == "+" {
                sign = 1
            }else if ch == "-"{
                sign = -1
            }else if ch == "("{
                debugPrint("stackapp:\(res) sign\(sign)")
                stack.append(res)
                res = 0
                stack.append(sign)
                sign = 1
            }else if ch == ")"{
//                res =  stack.dropLast(1).first! * res
                var last : Int = stack.last!
                stack.removeLast()
                res = last
                debugPrint("droplsat:\(res) ")
                
                var last2 : Int = stack.last!
                stack.removeLast()
                res = res + last2 //stack.dropLast(1).first!
                debugPrint("droplsat2:\(res) ")
            }
        }
        
        return res
    }
}
